home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP8.TXT < prev    next >
Text File  |  1987-11-21  |  19KB  |  455 lines

  1.  
  2.                             Chapter 8 - Pointers
  3.  
  4.  
  5.                              WHAT IS A POINTER?
  6.  
  7.              Simply  stated,  a pointer is an address.   Instead  of
  8.         being  a  variable,  it  is a pointer to a  variable  stored
  9.         somewhere in the address space of the program.  It is always
  10.         best to use an example so load the file named POINTER.C  and
  11.         display  it on your monitor for an example of a program with
  12.         some pointers in it.
  13.  
  14.              For  the moment,  ignore the data declaration statement
  15.         where we define "index" and two other fields beginning  with
  16.         a star.   It is properly called an asterisk, but for reasons
  17.         we  will see later,  let's agree to call it a star.   If you
  18.         observe  the  first statement,  it should be clear  that  we
  19.         assign the value of 39 to the variable "index".   This is no
  20.         surprise,  we  have been doing it for several programs  now.
  21.         The  next  statement  however,  says to assign  to  "pt1"  a
  22.         strange looking value,  namely the variable "index" with  an
  23.         ampersand in front of it.   In this example, pt1 and pt2 are
  24.         pointers,  and  the  variable "index" is a simple  variable.
  25.         Now we have a problem.  We need to learn how to use pointers
  26.         in a program, but to do so requires that first we define the
  27.         means of using the pointers in the program.
  28.  
  29.              The  following two rules will be somewhat confusing  to
  30.         you at first but we need to state the definitions before  we
  31.         can  use  them.   Take your time,  and the whole thing  will
  32.         clear up very quickly.
  33.  
  34.                           TWO VERY IMPORTANT RULES
  35.  
  36.              The  following two rules are very important when  using
  37.         pointers and must be thoroughly understood.
  38.  
  39.         1.  A variable name with an ampersand in front of it defines
  40.             the  address of the variable and therefore points to the
  41.             variable.  You can therefore read line seven as "pt1  is
  42.             assigned the value of the address of index".
  43.  
  44.         2.  A  pointer  with a "star" in front of it refers  to  the
  45.             value of the variable pointed to by the  pointer.   Line
  46.             ten  of the program can be read as "The stored (starred)
  47.             value  to which the pointer "pt1" points is assigned the
  48.             value  13".   Now  you can see why it is  convenient  to
  49.             think of the asterisk as a star,  it sort of sounds like
  50.             the word store.
  51.  
  52.                                   MEMORY AIDS
  53.  
  54.             1. Think of & as an address.
  55.             2. Think of * as a star referring to stored.
  56.  
  57.  
  58.                                   Page 52
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                             Chapter 8 - Pointers
  69.  
  70.  
  71.  
  72.              Assume for the moment that "pt1" and "pt2" are pointers
  73.         (we will see how to define them shortly).  As pointers, they
  74.         do not contain a variable value but an address of a variable
  75.         and  can  be  used to point to a variable.  Line  7  of  the
  76.         program  assigns the pointer "pt1" to point to the  variable
  77.         we have already defined as "index" because we have  assigned
  78.         the address of "index" to "pt1".  Since we have a pointer to
  79.         "index",  we  can manipulate the value of "index"  by  using
  80.         either the variable name itself, or the pointer.
  81.  
  82.              Line 10 modifies the value by using the pointer.  Since
  83.         the  pointer  "pt1"  points to the  variable  "index",  then
  84.         putting  a star in front of the pointer name refers  to  the
  85.         memory location to which it is pointing.  Line 10  therefore
  86.         assigns to "index" the value of 13. Anyplace in the  program
  87.         where it is permissible to use the variable name "index", it
  88.         is  also permissible to use the name "*pt1" since  they  are
  89.         identical in meaning until the pointer is reassigned to some
  90.         other variable.
  91.  
  92.                               ANOTHER POINTER
  93.  
  94.              Just  to add a little intrigue to the system,  we  have
  95.         another  pointer  defined in this  program,  "pt2".    Since
  96.         "pt2" has not been assigned a value prior to statement 8, it
  97.         doesn't point to anything, it contains garbage.  Of  course,
  98.         that is also true of any variable until a value is  assigned
  99.         to it. Statement 8 assigns "pt2" the same address as  "pt1",
  100.         so  that now "pt2" also points to the variable "index".   So
  101.         to continue the definition from the last paragraph, anyplace
  102.         in  the program where it is permissible to use the  variable
  103.         "index",  it  is  also permissible to use  the  name  "*pt2"
  104.         because  they   are  identical in  meaning.   This  fact  is
  105.         illustrated  in  the  first "printf"  statement  since  this
  106.         statement  uses  the  three means of  identifying  the  same
  107.         variable to print out the same variable three times.
  108.  
  109.                          THERE IS ONLY ONE VARIABLE
  110.  
  111.              Note carefully that,  even though it appears that there
  112.         are three variables, there is really only one variable.  The
  113.         two  pointers  point  to  the  single  variable.    This  is
  114.         illustrated in the next statement which assigns the value of
  115.         13  to  the  variable "index",  because that  is  where  the
  116.         pointer  "pt1"  is pointing.   The next  "printf"  statement
  117.         causes  the  new value of 13 to be printed out three  times.
  118.         Keep  in mind that there is really only one variable  to  be
  119.         changed, not three.
  120.  
  121.  
  122.  
  123.  
  124.                                   Page 53
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                             Chapter 8 - Pointers
  135.  
  136.  
  137.              This is admittedly a very difficult concept,  but since
  138.         it  is  used  extensively  in all but  the  most  trivial  C
  139.         programs,  it  is  well  worth your time to stay  with  this
  140.         material until you understand it thoroughly.
  141.  
  142.                         HOW DO YOU DECLARE A POINTER?
  143.  
  144.              Now  to  keep a promise and tell you how to  declare  a
  145.         pointer.   Refer  to the third line of the program  and  you
  146.         will  see  our  old familiar way of  defining  the  variable
  147.         "index",  followed  by  two more  definitions.   The  second
  148.         definition  can  be read as "the storage location  to  which
  149.         "pt1"  points  will be an int  type  variable".   Therefore,
  150.         "pt1" is a pointer to an int type variable.  Likewise, "pt2"
  151.         is another pointer to an int type variable.
  152.  
  153.              A  pointer  must  be defined to point to some  type  of
  154.         variable.   Following a proper definition, it cannot be used
  155.         to point to any other type of variable or it will result  in
  156.         a  "type incompatibility" error.   In the same manner that a
  157.         "float"  type of variable cannot be added to an  "int"  type
  158.         variable,  a pointer to a "float" variable cannot be used to
  159.         point to an integer variable.
  160.  
  161.              Compile and run this program and observe that there  is
  162.         only  one  variable  and the single  statement  in  line  10
  163.         changes the one variable which is displayed three times.
  164.  
  165.                       THE SECOND PROGRAM WITH POINTERS
  166.  
  167.              In these few pages so far on pointers,  we have covered
  168.         a lot of territory, but it is important territory.  We still
  169.         have  a  lot  of  material to cover so stay in  tune  as  we
  170.         continue  this important aspect of C.   Load the  next  file
  171.         named  POINTER2.C  and display it on your monitor so we  can
  172.         continue our study.
  173.  
  174.              In  this program we have defined several variables  and
  175.         two pointers.   The first pointer named "there" is a pointer
  176.         to  a "char" type variable and the second named "pt"  points
  177.         to an "int" type variable.  Notice also that we have defined
  178.         two  array variables named "strg" and "list".   We will  use
  179.